package com.liming.stock.controller;

import com.liming.common.enums.DateScopeEnum;
import com.liming.common.pojo.dto.stock.*;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.stock.ProductSalesCountVO;
import com.liming.common.pojo.vo.stock.StockDashboardVO;
import com.liming.common.pojo.vo.stock.StockFlowVO;
import com.liming.common.pojo.vo.stock.StockVO;
import com.liming.common.result.LMResult;
import com.liming.stock.service.StockFlowMircoService;
import com.liming.stock.service.StockMircoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/1/21
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "库存接口-Service")
@RequestMapping("/service/stock")
public class StockMircoController {

    private final StockMircoService stockMircoService;
    private final StockFlowMircoService stockFlowMircoService;

    @Operation(summary = "分页 - 库存记录")
    @GetMapping
    public LMResult<PageVO<StockVO>> stockVOList(StockPageQuery query) {
        return LMResult.success(stockMircoService.page(query));
    }

    @Operation(summary = "查询商品可用库存数量")
    @GetMapping("availableQuantity/{id}")
    public LMResult<Integer> getAvailableQuantity(@PathVariable Long id) {
        Integer result = stockMircoService.getAvailableQuantity(id);
        return LMResult.success(result);
    }

    @Operation(summary = "分页 - 所有库存流水记录")
    @GetMapping("flow")
    public LMResult<PageVO<StockFlowVO>> pageFlow(StockFlowPageQuery query) {
        return LMResult.success(stockFlowMircoService.pageFlow(query));
    }

    @Operation(summary = "库存流水大屏")
    @GetMapping("store-dashboard")
    public LMResult<StockDashboardVO> stockDashboard(@RequestParam String scope) {
        StockDashboardVO result = stockFlowMircoService.flowDashboard(scope);
        return LMResult.success(result);
    }

    @Operation(summary = "商品出库数量 - 时间")
    @PostMapping("salesCountByTime")
    public LMResult<List<ProductSalesCountVO>> salesCountByTime(@RequestBody DateScopeEnum dateScopeEnum) {
        return LMResult.success(stockFlowMircoService.salesCountByTime(dateScopeEnum));
    }

    @Operation(summary = "库存预警数量")
    @GetMapping("alertStockCount")
    public LMResult<Long> alertStockCount() {
        return LMResult.success(stockMircoService.alertStockCount());
    }

    @Operation(summary = "所有商品可用库存数量")
    @GetMapping("allProductAvailableQuantityMap")
    public LMResult<Map<Long, Integer>> allProductAvailableQuantityMap() {
        return LMResult.success(stockMircoService.allProductAvailableQuantityMap());
    }

    @Operation(summary = "热销商品IDS")
    @GetMapping("hotSalesProductIds")
    public LMResult<List<Long>> hotSalesProductIds() {
        return LMResult.success(stockFlowMircoService.hotSalesProductIds());
    }

    @Operation(summary = "初始化库存记录")
    @PostMapping
    public LMResult<Boolean> initStock(@RequestBody InitStockDTO initStockDTO) {
        Boolean result = stockMircoService.initStock(initStockDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "新增库存流水记录")
    @PostMapping("flow")
    public LMResult<Boolean> addStockFlow(@RequestBody AddStockFlowDTO addStockFlowDTO) {
        Boolean result = stockFlowMircoService.adminStockFlow(addStockFlowDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "退款成功")
    @PostMapping("refundSuccess/{orderId}")
    public void refundSuccess(@PathVariable Long orderId) {
        stockMircoService.refundSuccess(orderId);
    }

    @Operation(summary = "更新 - 总库存&预警库存")
    @PutMapping("updStockQuantity")
    public LMResult<Boolean> updStockQuantity(@RequestBody UpdStockQuantityDTO updStockQuantityDTO) {
        Boolean result = stockMircoService.updStockQuantity(updStockQuantityDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "更新 - 启用|禁用")
    @PutMapping("updStatus/{id}")
    public LMResult<Boolean> updStatus(@PathVariable Long id) {
        Boolean result = stockMircoService.updStatus(id);
        return LMResult.success(result);
    }

    @Operation(summary = "更新 - 出库")
    @PutMapping("out/{id}")
    public LMResult<Boolean> out(@PathVariable Long id, @RequestParam Integer quantity) {
        Boolean result = stockMircoService.out(id, quantity);
        return LMResult.success(result);
    }

    @PutMapping("lockStockByProductIdAndQuantity/{orderId}")
    public LMResult<Boolean> lockStockByProductIdAndQuantity(@PathVariable Long orderId, @RequestBody Map<Long, Integer> productIdAndQuantityMap) {
        return LMResult.success(stockMircoService.lockStockByProductIdAndQuantity(orderId, productIdAndQuantityMap));
    }

    @PutMapping("paymentSuccess/{orderId}")
    public LMResult<Boolean> paymentSuccess(@PathVariable Long orderId) {
        return LMResult.success(stockMircoService.paymentSuccess(orderId));
    }

    @PutMapping("paymentFailure/{orderId}")
    public LMResult<Boolean> paymentFailure(@PathVariable Long orderId) {
        return LMResult.success(stockMircoService.paymentFailure(orderId));
    }
}
